Skip to content

Semantic check of mut restrictions - #159906

Open
CoCo-Japan-pan wants to merge 20 commits into
rust-lang:mainfrom
CoCo-Japan-pan:mut-restriction-check
Open

Semantic check of mut restrictions#159906
CoCo-Japan-pan wants to merge 20 commits into
rust-lang:mainfrom
CoCo-Japan-pan:mut-restriction-check

Conversation

@CoCo-Japan-pan

@CoCo-Japan-pan CoCo-Japan-pan commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

View all comments

This PR implements semantic checks for mut restrictions proposed in RFC 3323, as part of the related GSoC project.
It lowers field restriction information from HIR into rustc_middle::ty::FieldDef and adds a MIR-level lint that rejects restricted field mutations and struct expressions. As parsing (#156824) and path resolution (#159125) have already been implemented, this PR provides a working implementation of mut restrictions.

Tracking issue: #105077
r? @Urgau
cc @jhpratt

@rustbot

rustbot commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 25, 2026
@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label Jul 25, 2026
@CoCo-Japan-pan CoCo-Japan-pan changed the title Mut restriction check Semantic check of mut restrictions Jul 25, 2026

@Urgau Urgau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start, mostly some tests related comments.

View changes since this review

Comment thread compiler/rustc_hir_analysis/src/collect.rs Outdated
Comment on lines 415 to 421
// MIR-level lints.
&Lint(check_inline::CheckForceInline),
&Lint(check_call_recursion::CheckCallRecursion),
&Lint(check_packed_ref::CheckPackedRef),
&Lint(check_const_item_mutation::CheckConstItemMutation),
&Lint(check_mut_restriction::CheckMutRestriction),
&Lint(function_item_references::FunctionItemReferences),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not for this PR) Most of those so called "MIR lints" actually emit hard errors of an lint. We should find another name as it's quite confusing to see a "lint" emit a hard errors.

Comment thread compiler/rustc_mir_transform/src/check_mut_restriction.rs Outdated
Comment thread compiler/rustc_mir_transform/src/check_mut_restriction.rs
Comment thread compiler/rustc_mir_transform/src/check_mut_restriction.rs
Comment thread tests/ui/mut-restriction/mut-restriction-construction.rs Outdated
Comment thread tests/ui/mut-restriction/mut-restriction-check.rs
Comment thread tests/ui/mut-restriction/mut-restriction-check.rs
Comment thread tests/ui/mut-restriction/mut-restriction-check.rs
Comment on lines +179 to +205
fn change_fooe(foo: &mut foo::bar::FooE) {
match foo {
foo::bar::FooE::Default => {}
foo::bar::FooE::Var {
alpha, //[e2015]~ ERROR field cannot be mutated outside `foo::bar`
//[e2018]~^ ERROR field cannot be mutated outside `crate::foo::bar`
beta, //[e2015]~ ERROR field cannot be mutated outside `foo`
//[e2018]~^ ERROR field cannot be mutated outside `crate::foo`
gamma, // ok
} => {
*alpha = 1;
*beta = 1;
*gamma = 1;
}
foo::bar::FooE::Tup(
alpha, //[e2015]~ ERROR field cannot be mutated outside `foo::bar`
//[e2018]~^ ERROR field cannot be mutated outside `crate::foo::bar`
beta, //[e2015]~ ERROR field cannot be mutated outside `foo`
//[e2018]~^ ERROR field cannot be mutated outside `crate::foo`
gamma, // ok
) => {
*alpha = 1;
*beta = 1;
*gamma = 1;
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nadrieril, can you think of another match related tests we should be testing regarding mut-restrictions?

@Urgau Urgau added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2026
@Urgau

Urgau commented Jul 25, 2026

Copy link
Copy Markdown
Member

cc @cjgillot for the MIR related part since you know more about it than me

@CoCo-Japan-pan

Copy link
Copy Markdown
Contributor Author

@rustbot ready
Sorry for the delay. I’ve added some tests to address the remaining comments. If everything looks good, I’ll squash the commits.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 28, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 28, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread tests/ui/mut-restriction/mut-restriction-check-asm.rs Outdated
Comment thread tests/ui/mut-restriction/mut-restriction-check-asm.rs Outdated
Comment thread tests/ui/mut-restriction/mut-restriction-check-asm.stderr Outdated
@CoCo-Japan-pan
CoCo-Japan-pan force-pushed the mut-restriction-check branch from f99a126 to 673a9db Compare July 29, 2026 13:46

@Urgau Urgau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

As before, I will @jhpratt do his review.

View changes since this review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants